package itheima.com.Properties;

import org.junit.Test;

import java.io.*;
import java.util.Properties;

public class Homework {

    public static void main(String[] args) throws IOException, InterruptedException {

//        work01 work01 = new work01();
//        work01.getFliePath();

//        work02 work02 = new work02();
//        work02.setRowNumber();

        work03 work03 = new work03();
        work03.getProperties();


    }


    @Test
    public void m1() throws IOException, ClassNotFoundException {
        String filePath = "D:\\一条小团团\\dog.dat";
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
        Dog dog = (Dog) objectInputStream.readObject();

        System.out.println("==========反序列化后 dog=========");
        System.out.println(dog);
        objectInputStream.close();
    }


}


class work01{

    private String fliePath = "D:\\一条小团团\\666.txt";
    private File flie;
//    priv
    public void getFliePath() throws IOException, InterruptedException {

       flie = new File(fliePath);

       if (!flie.exists()){
           if(flie.createNewFile()){
               System.out.println(fliePath+"创建成功~");

               BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fliePath));
               System.out.println("正在写入");
               bufferedWriter.write("手机壳电力设计阿卡丽高发季洒基好卡等哈就撒离开家分开旅行久啊山卡拉送达给国际大发拉卡萨进啦。。");
               bufferedWriter.close();
               System.out.println("正在写入关闭。。。。");
//             try (BufferedReader bufferedReader = new BufferedReader(new FileWriter(fliePath))) {
//                   bufferedReader.wait("李丢丢的可视电话杰卡斯浩丰科技山卡拉就撒建行付款手机卡的金三发尽快核实那几科");
//                   bufferedReader.close();
//             }

           }else {
               System.out.println(fliePath+"创建失败~");
           }
       }else {
           System.out.println(fliePath+"文件已存在，不在重复创建~~~~~");
       }

    }


}

class work02{
    private String filePath = "D:\\一条小团团\\demo.txt";
    private BufferedReader bfr = null;
    private String line;
    private BufferedWriter bfw = null;


    public void setRowNumber() throws IOException {
        bfr = new BufferedReader(new FileReader(filePath));
        bfw = new BufferedWriter(new FileWriter(filePath,true));
        String temp = "";
        int lineNum = 0;
        try {
            while ((line = bfr.readLine()) != null) {
                temp = line + "→→→→→→→→→";
                System.out.println( temp );
                bfw.write(temp);
//                System.out.println(++lineNum + line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bfw.close();
                bfr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }





}

class work03{

   public void getProperties() throws IOException {
       String filePath = "src\\dog.properties";
       Properties properties = new Properties();
       properties.load(new FileReader(filePath));
       String name = properties.get("name") + "";
       int age = Integer.parseInt(properties.get("age") +"");
       String color = properties.get("color") + "";

       Dog dog = new Dog(name, age, color);
       System.out.println("======= dog对象信息 =======");
       System.out.println(dog);

       String serFilePath = "D:\\一条小团团\\dog.dat";
       ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(serFilePath));
       objectOutputStream.writeObject(dog);

       objectOutputStream.close();
       System.out.println("dog对象，序列化完成...");
   }

}








class Dog implements Serializable{
    private String name;
    private int age;
    private String color;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }

    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }
}


